package com.bw.controller;

import com.alibaba.excel.EasyExcel;
import com.bw.entity.Bing;
import com.bw.entity.Zxt;
import com.bw.mapper.ZxtMapper;
import org.spring.kun.common.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@Controller
public class TuController {

    @Autowired
    ZxtMapper zxtMapper;

    @RequestMapping(value = "/tu.do")
    public String tu() {

        return "tu";
    }

    /**
     * 查询折线图、 柱状图 需要的数据
     *
     * @return
     */
    @RequestMapping(value = "/zxt.do")
    @ResponseBody  // 把返回值 变成JSON字符串
    public List<Zxt> zxt(String g) {
        List<Zxt> list = zxtMapper.zxt(g);
        return list;
    }
    /**
     * 查询折线图需要的数据
     *
     * @return
     */
    @RequestMapping(value = "/bing.do")
    @ResponseBody  // 把返回值 变成JSON字符串
    public List<Bing> bing(String group) {
        List<Bing> list = zxtMapper.bing(group);
        return list;
    }


    /**
     * 导出Excel
     *
     * @return
     */
    @RequestMapping(value = "/excel.do")
    public void excel(String group, HttpServletResponse response) throws Exception {
        List<Bing> list = zxtMapper.bing(group);

      /*
      // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("学生数量导出", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        // 导出Excel
        EasyExcel.write(response.getOutputStream(), Bing.class).sheet("sheet1").doWrite(list);
        */

        ExcelUtils.exportExcel(list,"学生数量导出","sheet11",response);
    }
}
